package cat.joronya.supermanager.europeo.service;

import android.content.Context;
import android.database.Cursor;
import cat.joronya.supermanager.europeo.Supermanager;

/**
 * This class check user data downloaded from supermanager
 * web. If inconsistent data found call for data sync.
 * @author pol
 */
public class UserDataManager 
{
	/**
	 * Checks data and point if local data needs update.
	 * -Next day is different as last data day of my team detail
	 * -Next day is different as last data day of market
	 * @return
	 */
	public static boolean appNeedsUpdate(Context context)
	{
		// if market needs update, return true
		if( marketNeedsUpdate(context))
			return true;
		
		// if my teams needs update, return true
		if( myTeamsNeedsUpdate(context))
			return true;
		
		// if private leagues needs update, return true
		if( privateLeagueTeamsNeedsUpdate(context))
			return true;
		
		// otherwise no need of update
		return false;
	}
	
	/**
	 * Given an app context, returns last day in last authentication process saved
	 * in local database.
	 * @param context
	 * @return
	 */
	public static Integer getDayHomeData(Context context)
	{
		Integer day = null;
		
		//TODO: delete this code. Is here for debug purposes to force data inconsistency
		//context.getContentResolver().delete(Supermanager.Market.CONTENT_URI,
		//		" day = ? ", new String[]{"14"});
		
		// query Home db
		Cursor cursor = context.getContentResolver().query(Supermanager.Home.CONTENT_URI, 
	        	new String[]{Supermanager.Home.DAY}, 
	           	null,
	           	null,
	            Supermanager.Home.DEFAULT_SORT_ORDER);
		
		// get just first register, and get duetime day
		if( cursor.moveToFirst() )
		{
			day = cursor.getInt(0);
		}
		
		// close cursor
		cursor.close();
		
		return day;
	}
	
	/**
	 * Given an app context, returns last next day in last authentication process saved
	 * in local database.
	 * @param context
	 * @return
	 */
	public static Integer getNextDayHomeData(Context context)
	{
		Integer duetimeDay = null;
		
		//TODO: delete this code. Is here for debug purposes
		//context.getContentResolver().delete(Supermanager.Market.CONTENT_URI,
		//		" day = ? ", new String[]{"14"});
		
		// query Home db
		Cursor cursor = context.getContentResolver().query(Supermanager.Home.CONTENT_URI, 
	        	new String[]{Supermanager.Home.NEXT_DAY}, 
	           	null,
	           	null,
	            Supermanager.Home.DEFAULT_SORT_ORDER);
		
		// get just first register, and get duetime day
		if( cursor.moveToFirst() )
		{
			duetimeDay = cursor.getInt(0);
		}
		
		// close cursor
		cursor.close();
		
		return duetimeDay;
	}
	
	/**
	 * Given an app context, returns supermanager status in last authentication process saved
	 * in local database.
	 * @param context
	 * @return
	 */
	public static Integer getStatusHomeData(Context context)
	{
		Integer status = null;
		
		// query Home db
		Cursor cursor = context.getContentResolver().query(Supermanager.Home.CONTENT_URI, 
	        	new String[]{Supermanager.Home.STATUS}, 
	           	null,
	           	null,
	            Supermanager.Home.DEFAULT_SORT_ORDER);
		
		// get just first register, and get duetime day
		if( cursor.moveToFirst() )
		{
			status = cursor.getInt(0);
		}
		
		// close cursor
		cursor.close();
		
		return status;
	}
	
	/**
	 * Given an app context, compare last day and market day of data.
	 * Returns true if home last day < last market day
	 * @param context
	 * @return
	 */
	public static boolean marketNeedsUpdate(Context context)
	{
		// get status
		Integer homeStatus = getStatusHomeData(context);
		if( Supermanager.OPEN_STATUS != homeStatus )
			return false;
		
		// get gome last day
		Integer homeDay = getNextDayHomeData(context);
		
		// init market day
		Integer marketDay = null;
		
		// query market to get one register of last day
		Cursor cursor = context.getContentResolver().query(Supermanager.Market.CONTENT_URI, 
	        	new String[]{Supermanager.Market.DAY}, 
	           	null,
	           	null,
	            Supermanager.Market.DAY_SORT_ORDER);
		
		if( cursor.moveToFirst() )
		{
			marketDay = cursor.getInt(0);
		}
		
		// close cursor
		cursor.close();
		
		// if no home day found, we should need market data refresh
		if( homeDay == null )
			return true;
		
		// if no market day found, we should  need market data refresh
		if( marketDay == null)
			return true;
		
		// if market day < home day, we need update
		if( marketDay.compareTo(homeDay) < 0 )
			return true;
		
		// otherwise, we have some data. Maybe not up to date, but app will not crash.
		return false;
	}
	
	/**
	 * Given an app context, compare last day and my team day of data.
	 * Returns true if home last day < last my team day
	 * @param context
	 * @return
	 */
	public static boolean myTeamsNeedsUpdate(Context context)
	{
		// get gome last day
		Integer homeDay = getNextDayHomeData(context);
		
		// init my team day
		Integer myTeamDay = null;
		
		// query market to get one register of last day
		Cursor cursor = context.getContentResolver().query(Supermanager.MyTeam.CONTENT_URI, 
	        	new String[]{Supermanager.MyTeam.DAY}, 
	           	null,
	           	null,
	            Supermanager.MyTeam.DAY_SORT_ORDER);
		
		boolean hasItems;
		if( hasItems = cursor.moveToFirst() )
		{
			myTeamDay = cursor.getInt(0);
		}
		
		// close cursor
		cursor.close();
		
		// if no items, no need to check
		if( !hasItems )
			return false;
		
		// if no home day found, we should need my teams data refresh
		if( homeDay == null )
			return true;
		
		// if no my teams day found, we should  need market data refresh
		if( myTeamDay == null)
			return true;
		
		// if my teams day < home day, we need update
		if( myTeamDay.compareTo(homeDay) < 0 )
			return true;
		
		// otherwise, we have some data. Maybe not up to date, but app will not crash.
		return false;
	}

	/**
	 * Given an app context, compare last day and private leagues day of data.
	 * Returns true if home last day < last private leagues day
	 * @param context
	 * @return
	 */
	public static boolean privateLeagueTeamsNeedsUpdate(Context context)
	{
		// get gome last day
		Integer homeDay = getDayHomeData(context);
		
		// init private league day
		Integer privateLeagueTeamDay = null;
		
		// query market to get one register of last day
		Cursor cursor = context.getContentResolver().query(Supermanager.PrivateLeagueTeam.CONTENT_URI, 
	        	new String[]{Supermanager.PrivateLeagueTeam.DAY}, 
	           	null,
	           	null,
	            Supermanager.PrivateLeagueTeam.DAY_SORT_ORDER);
		
		boolean hasItems;
		if( hasItems = cursor.moveToFirst() )
		{
			privateLeagueTeamDay = cursor.getInt(0);
		}
		
		// close cursor
		cursor.close();
		
		// if no items, no need to check
		if( !hasItems )
			return false;
		
		// if no home day found, we should need my teams data refresh
		if( homeDay == null )
			return true;
		
		// if no private league team day found, we should  need market data refresh
		if( privateLeagueTeamDay == null)
			return true;
		
		// if private league team day < home day, we need update
		if( privateLeagueTeamDay.compareTo(homeDay) < 0 )
			return true;
		
		// otherwise, we have some data. Maybe not up to date, but app will not crash.
		return false;
	}
}
